
//--> Loth 2011 
//--> Light systeme day cycle 0.1a

package chamans.away
{
	import away3d.containers.*;
	import away3d.entities.Sprite3D;
	import away3d.lights.*;
	import away3d.materials.BitmapMaterial;
	import away3d.materials.methods.SoftShadowMapMethod;
	import away3d.primitives.Plane;
	
	import flash.display.BitmapData;
	import flash.display.GradientType;
	import flash.display.Shape;
	import flash.geom.*;
	
	public class light 
	{
		internal var BASE:Object
		public var center:Vector3D;
		public var angle:Number;
		public var angle2:Number;
		public var sundistance:Number=0
	    public var shad : SoftShadowMapMethod
		public var SUN:LightBase
		public var MOON:LightBase
		internal var SUNmap:BitmapMaterial
		internal var MOONmap:BitmapMaterial
		internal var sunsize:Number
		public function light(base:Object):void
		{
			BASE=base
			BASE.LIGHT=new Array()
			config_base()
		}
		public function config_base():void
		{
            sunsize =BASE.view.camera.lens.far/10;
			SUN = add( "sun", 0xFFFFFE, 0, -1, 0, 5, 1, true );
			DayCycle(8 , 110);
		}
		public function Orbit( H:Number, V:Number, D:int ):Vector3D 
		{
			var P:Vector3D=new Vector3D()
			var phi:Number= RadDeg(H)
			var theta:Number= RadDeg(V)
			P.x = (D * Math.sin(phi)*Math.cos(theta));
			P.z = (D * Math.sin(phi)*Math.sin(theta));
			P.y = (D * Math.cos(phi));
			return P;
		}
		public function RadDeg(d:Number):Number{ return(d * (Math.PI / 180)) }

		
		public function add(type:String, c:int, x:int, y:int, z:int, d:Number=1, s:Number=1, H:Boolean=false, target:*=null, visible:Boolean=false, RAD:int=100, FAL:int=100 ):LightBase
		{
			
			var l:LightBase;
			var g:PointLight;
			var h:DirectionalLight;
			switch(type){
				case "sun":h = new DirectionalLight(x, y, z); h.addChild(Visible(h, c, type, sunsize)); break; 
				case "moon":h = new DirectionalLight(x, y, z); h.addChild(Visible(h, c, type, sunsize/1.5)); break; 
				case "direct":h = new DirectionalLight(x, y, z); break; 
				case "point":g = new PointLight();if(visible) g.addChild(Visible(g, c, "", RAD)); 
					g.radius=RAD;
					g.fallOff=FAL;
					g.moveTo(x, y, z); break; 
			}
			if(h){
			h.color = c;
			h.specular = s;
			h.diffuse = d;
			h.castsShadows = H;
			if(target)target.addChild(h);
			else BASE.view.scene.addChild(h);
			l=h;
			}
			if(g){
				g.color = c;
				g.specular = s;
				g.diffuse = d;
				g.castsShadows = H;
				if(target)target.addChild(g);
				else BASE.view.scene.addChild(g);
				l=g;
			}
			l.name=type;
			BASE.LIGHT.push(l);
			return l
		}
		internal function Visible( Light:LightBase, c:int, type:String="", size:int=100 ):ObjectContainer3D
		{
			var tex:BitmapData=BASE.TOOL.bit(cercle(c, 1, 60,60,2,2,true, 1), 64, 64, 2,true)
			var mat:BitmapMaterial =  new BitmapMaterial(tex);
		 
			mat = new BitmapMaterial(tex);
			mat.transparent=true;
			mat.blendMode="add";
			mat.bothSides=false;
			mat.blendMode="add";
		
			if(type=="sun")SUNmap = mat;
		    if(type=="moon"){MOONmap = mat; MOONmap.alpha=0;}
			var M:ObjectContainer3D=new ObjectContainer3D();
			var lens:Sprite3D=new Sprite3D(mat, size,size);
			M.addChild(lens);
			return M;
		}
		private function cercle( c:int, a:Number=1, w:int=100, h:int=100, x:int=0, y:int=0, t:Boolean=false, l:int=0):Shape
		{
			var g:Shape=new Shape;
			var mtx:Matrix = new Matrix();
			mtx.createGradientBox(w,h,0,0,0);
			g.graphics.beginGradientFill(GradientType.RADIAL, [c, c, c], [1,.5,0], [0,180,255], mtx);
			g.graphics.drawEllipse(x,y,w,h);
			g.graphics.endFill();
			return g;
		}
		public function update( ):void
		{
			DayCycle(angle , angle2);
		}
		public function DayCycle(aa:Number=0, bb:Number=0 ):void
		{
		 	angle = aa;
			angle2 = bb;
			center=BASE.Camera.M.baseTarget;
			SUN.position=Orbit(angle, angle2, BASE.view.camera.lens.far-1000).add(center);
			SUN.lookAt(center);
			var t:Number= angle/15	+ 12;
			var time:int= int(t);
			var min:int = ( t-time )*60;
			var a : int = SUNmap.alpha*10;
			if(angle>360) angle=0;
			if(angle>90 && angle<270 && a!=0 ) a--; 
			if(angle>270 && a!=10) a++;
			if(time> 23 ) time-= 24;
					
			SUNmap.alpha=a/10;
			BASE.LIGHT[0].diffuse=(a/10);
			BASE.LIGHT[0].specular=(a/10);
		}
//END
}}